package com.liaowei.comment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liaowei.comment.model.param.PostCommentParam;
import com.liaowei.comment.service.PostCommentService;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.param.QueryManagePostCommentParam;
import com.liaowei.model.vo.PostCommentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("posts")
@RequiredArgsConstructor
@Validated
public class PostCommentController {

    private final PostCommentService postCommentService;

    // 后台-分页查询评论列表
    @GetMapping("manage/page")
    public BaseResponse<IPage<PostCommentVO>> managePage(int pageNum, int pageSize) {
        return ResultUtils.success(postCommentService.searchManagePage(pageNum,pageSize));
    }


    // 分页查询评论列表
    @PostMapping("page")
    public BaseResponse<IPage<PostCommentVO>> page(@RequestBody
                                                   @Validated
                                                   QueryManagePostCommentParam commentParam) {
        return ResultUtils.success(postCommentService.searchPage(commentParam));
    }

    // 根据帖子id删除帖子的评论
    @DeleteMapping("/{postId}/{userId}")
    public BaseResponse<String> deleteComment(@PathVariable("postId") @NotNull(message = "文章id不能为空") Long postId,
                                              @PathVariable("userId") @NotNull(message = "评论id不能为空") Long userId) {
        return ResultUtils.success(postCommentService.deletePostCommentByIdAndUserId(postId, userId));
    }


    // 删除评论
    @DeleteMapping("{commentId}/{postId}/{userId}")
    public BaseResponse<String> deleteComment(@PathVariable("commentId") @NotNull(message = "评论id不能为空") Long commentId,
                                              @PathVariable("postId") @NotNull(message = "帖子id不能为空") Long postId,
                                              @PathVariable("userId") @NotNull(message = "评论id不能为空") Long userId) {
        return ResultUtils.success(postCommentService.deletePostComment(commentId, postId, userId));
    }

    // 评论帖子
    @PostMapping
    public BaseResponse<String> commentPost(@RequestBody @Validated PostCommentParam commentParam) {
        return ResultUtils.success(postCommentService.savePostComment(commentParam));
    }
}
